<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="doAdd('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形组件 -->
        <el-tree :data="list" default-expand-all @node-click="handleNodeClick">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="doAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="doUpdate(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length === 0" @click.native="doDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 弹层盒子 -->
      <el-dialog
        v-if="showDialog"
        title="添加或编辑"
        :visible.sync="showDialog"
      >
        <addor-edit
          :id="curId"
          :is-edit="isEdit"
          :origin-list="originList"
          @closePop="closepop"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getdepartment, deldepartmentList } from '@/api/department'
import { treeString } from '@/utils'
import addorEdit from './deptDialog.vue'
export default {
  components: {
    addorEdit
  },
  data() {
    return {
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    // 在钩子中触发
    this.doGet()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    async doGet() {
      // 获取组织架构数据
      const res = await getdepartment()
      res.data.depts.shift()
      this.list = treeString(res.data.depts)
      this.originList = res.data.depts.map((item) => {
        return { code: item.code, id: item.id, pid: item.pid, name: item.name }
      })

      console.log(res.data.depts, '组织架构数据')
    },

    // 点击新增按钮
    doAdd(id) {
      // 打开弹层
      this.showDialog = true
      // 获取id存储到data中
      this.curId = id
      // 判断是否为编辑页面
      this.isEdit = false
    },

    // 点击编辑按钮
    doUpdate(id) {
      // 打开弹层
      this.showDialog = true
      // 获取id存储到data中
      this.curId = id
      // 判断是否为编辑页面
      this.isEdit = true
    },

    // 关闭弹层(子传父)
    closepop() {
      this.doGet()
      this.showDialog = false
    },

    // 删除
    doDel(id) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deldepartmentList(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.doGet()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
